﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;

namespace FileSystem
{
    public class Dir
    {

        public string[] Tree_Directories;
        public string[] Tree_Files;
        public string Tree_TarDir;
        public string Tree_Root;

        private void Load_Tree(string folder)
        {

            string[] fdr = folder.Split('\\');
            Tree_TarDir = fdr[fdr.Length - 1];
            Tree_Root = Convert.ToString(Directory.GetParent(folder));
            string[] OI;
            string[] NI;

            Tree_Directories = Directory.GetDirectories(folder);
            Tree_Files = Directory.GetFiles(folder);

            for (int i = 0; i < Tree_Directories.Length; i++)
            {
                OI = new string[Tree_Directories.Length];
                NI = new string[Directory.GetDirectories(Tree_Directories[i]).Length];
                NI = Directory.GetDirectories(Tree_Directories[i]);

                for (int c = 0; c < Tree_Directories.Length; c++)
                    OI[c] = Tree_Directories[c];

                Tree_Directories = new string[Tree_Directories.Length + NI.Length];

                for (int c = 0; c < OI.Length; c++)
                    Tree_Directories[c] = OI[c];

                for (int c = OI.Length; c < NI.Length + OI.Length; c++)
                    Tree_Directories[c] = NI[c - OI.Length];
            }

            for (int i = 0; i < Tree_Directories.Length; i++)
            {
                OI = new string[Tree_Files.Length];
                NI = new string[Directory.GetFiles(Tree_Directories[i]).Length];
                NI = Directory.GetFiles(Tree_Directories[i]);

                for (int c = 0; c < Tree_Files.Length; c++)
                    OI[c] = Tree_Files[c];

                Tree_Files = new string[Tree_Files.Length + NI.Length];

                for (int c = 0; c < OI.Length; c++)
                    Tree_Files[c] = OI[c];

                for (int c = OI.Length; c < NI.Length + OI.Length; c++)
                    Tree_Files[c] = NI[c - OI.Length];
            }

            for (int i = 0; i < Tree_Directories.Length; i++)
                Tree_Directories[i] = Tree_Directories[i].Substring(Tree_Root.Length, Tree_Directories[i].Length - Tree_Root.Length);
            for (int i = 0; i < Tree_Files.Length; i++)
                Tree_Files[i] = Tree_Files[i].Substring(Tree_Root.Length, Tree_Files[i].Length - Tree_Root.Length);


        }
        public void Copy_Tree(string source, string destination, bool includeFileStructure, bool overwrite)
        {
            Load_Tree(source);
            if (includeFileStructure == true)
            {
                if (Tree_Directories.Length == 0)
                    Directory.CreateDirectory(destination + "\\" + Tree_TarDir);

                for (int i = 0; i < Tree_Directories.Length; i++)
                    Directory.CreateDirectory(destination + Tree_Directories[i]);
                for (int i = 0; i < Tree_Files.Length; i++)
                    File.Copy(Tree_Root + Tree_Files[i], destination + Tree_Files[i], true);
            }
            else
            {
                if (Tree_Directories.Length == 0)
                    Directory.CreateDirectory(destination + "\\" + Tree_TarDir);

                for (int i = 0; i < Tree_Directories.Length; i++)
                    Directory.CreateDirectory(destination + Tree_Directories[i]);
            }
        }
        public void Rename_Directory(string folder, string newName)
        {
            Load_Tree(folder);
            string[] path;
            string newPath = "";
            string EF;
            string[] NP = new string[Tree_Directories.Length];
            string[] NF = new string[Tree_Files.Length];
            path = folder.Split('\\');
            path[path.Length - 1] = newName;

            for (int i = 0; i < path.Length; i++)
                newPath += path[i] + "\\";

            for (int i = 0; i < Tree_Directories.Length; i++)
            {
                EF = Tree_Directories[i].Substring(Tree_TarDir.Length + 2, Tree_Directories[i].Length - Tree_TarDir.Length - 2);
                NP[i] = newPath + EF;
            }

            for (int i = 0; i < Tree_Files.Length; i++)
            {
                EF = Tree_Files[i].Substring(Tree_TarDir.Length + 2, Tree_Files[i].Length - Tree_TarDir.Length - 2);
                NF[i] = newPath + EF;
            }

            for (int i = 0; i < NP.Length; i++)
                Directory.CreateDirectory(NP[i]);
            for (int i = 0; i < NF.Length; i++)
                File.Copy(Tree_Root + Tree_Files[i].Substring(1, Tree_Files[i].Length - 1), NF[i], true);

            Directory.Delete(Tree_Root + "\\" + Tree_TarDir, true);

        }
    }
}
